﻿/**
 * Remoting Class - Created by FlashFrameLess 2008
 *
 * @author		FlashFrameLess - http://www.flashframeless.com/
 * @author		FabioTNT - http://www.fabiotnt.com/ - FlashFrameLess Team
 * @version		1.0
 */

package com.flashframeless.net {
	
	import flash.net.NetConnection;
	import flash.net.ObjectEncoding;
	import flash.net.Responder;
	
	public class AMFPHP {
		
		private static var _hostUrl 	: String;
		
		private static var _conection	: NetConnection;
		private static var _responder	: Responder;
		
		public function AMFPHP(){
			
			trace ( "AMFPHP is a static class and should not be instantiated." );
			
		}
		
		public static function set host( hostAddress:String ):void {
			
			_hostUrl = hostAddress;
			
			//Init AMF-PHP
			_conection = new NetConnection();
			_conection.objectEncoding = ObjectEncoding.AMF3;
			
			_conection.connect( _hostUrl );
			
		}
		
		public static function call( callFunction:String, responseFunction:Function, extraArgs:Array = null ) {
			
			var i		: uint;
			var argArr	: Array;
			
			_responder = new Responder( responseFunction, faultResponse );
			
			argArr = new Array( callFunction, _responder );
			
			for ( i = 0; i < extraArgs.length; i++ ) {
				
				argArr.push( extraArgs[i] );
				
			}
			
			_conection.call.apply( _conection, argArr );
			
		}
		
		private static function faultResponse( result:Object ) {
			
			trace( "fault: " );
			
			for ( var i:* in result ) {
				
				trace( result[i] );
				
			}
			
		}
		
	}
	
}